package cn.wenhaha.common.utils;
import cn.hutool.core.util.StrUtil;
import cn.wenhaha.common.core.domain.AjaxResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName: ResponseHtml
 * @description: TODO
 * @author: wyndem
 * @date: 2020/3/8 17:25
 * @Version: 1.0
 **/
public class ResponseMsg {

    public static Object error(HttpServletRequest request, String msg){
        return result(request,AjaxResult.error(msg));
    }


    public static Object success(HttpServletRequest request,String msg, Object data){
        return result(request, AjaxResult.success(msg,data));
    }



    public static Object result(HttpServletRequest request,AjaxResult result){
        String header = request.getHeader("Accept");
        if (StrUtil.contains(header,"text/html")){
            Set<String> keySet = result.keySet();
            Iterator<String> iterator = keySet.iterator();
            while (iterator.hasNext()){
                String key = iterator.next();
                if (key.equals(AjaxResult.URL_TAG)){
                    Object o=result.get(key);
                    if (o instanceof  String){
                        String url=(String)o;
                        if (StrUtil.isEmpty(url)){
                            result.put(AjaxResult.URL_TAG,"javascript:history.back(-1);");
                        }
                    }
                }
                request.setAttribute(key,result.get(key));
            }
            return "dispatch_jump";
        }
        return result;
    }




}
